package com.example.service.impl;

import com.example.dao.ActivityDao;
import com.example.entity.Activity;
import com.example.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;


@Service
@Transactional
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public Activity getActivityById(Integer activityId) {
        return activityDao.getActivityById(activityId);
    }
    @Override
    public List<Activity> getActivitiesByPage(int page, int pageNum) {
        int startIndex = (page - 1) * pageNum;
        return activityDao.findActivitiesByPage(startIndex, pageNum);
    }

    @Override
    public int getTotalPages(int pageNum) {
        int total = activityDao.countAllActivities();
        return (int) Math.ceil(total / (double) pageNum);
    }

    @Override
    public int getActivityCountByOrganization(int orgId) {
        return activityDao.countByOrganizationId(orgId);
    }

    @Override
    public Activity getLatestActivity(int orgId) {
        return activityDao.findTopByOrganizationIdOrderByCreateTimeDesc(orgId);
    }



    @Override
    public int countActivitiesByOrgId(int orgId) {
        return activityDao.countActivitiesByOrgId(orgId);
    }

    @Override
    public Activity findLatestByOrgId(int orgId) {
        return activityDao.findLatestByOrgId(orgId); // 需要 MyBatis Mapper 支持
    }


    @Override
    public int[] getMonthlyActivityData(int orgId) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            List<Integer> list = sqlSession.selectList("getMonthlyActivityData", orgId);
            return list.stream().mapToInt(Integer::intValue).toArray();
        }

    }
    @Override
    public int getTotalServiceHours(int orgId) {
        return activityDao.getTotalServiceHours(orgId);
    }
    @Override
    public List<Activity> getAllActivitiesByOrgId(int orgId) {
        return activityDao.getAllActivitiesByOrgId(orgId);
    }

    @Override
    public List<Activity> getOngoingActivitiesByOrgId(int orgId) {
        return activityDao.getOngoingActivitiesByOrgId(orgId);
    }

    @Override
    public List<Activity> getUpcomingActivitiesByOrgId(int orgId) {
        return activityDao.getUpcomingActivitiesByOrgId(orgId);
    }

    @Override
    public List<Activity> getCompletedActivitiesByOrgId(int orgId) {
        return activityDao.getCompletedActivitiesByOrgId(orgId);
    }





    @Override
    public void saveActivity(Activity activity) {
        activityDao.saveActivity(activity);
    }

    @Override
    public void addActivity(Activity activity) {
        activityDao.saveActivity(activity);
    }
    @Override
    public void createActivity(Activity activity) {
        activityDao.createActivity(activity);
    }
    @Override
    public Activity getActivityById(int activityId) {

        return activityDao.getActivityById(activityId);
    }

    @Override
    @Transactional // 确保事务管理
    public Activity updateActivity(Activity activity) {
        System.out.println("准备更新的活动: " + activity);
        // 确保DAO方法正确更新数据
        activityDao.updateActivity(activity);
        return activity;
    }
    @Override
    public void deleteActivity(int activityId) {
        System.out.println("准备删除的活动 ID11111: " + activityId);
        activityDao.deleteActivity(activityId);
    }


}
